﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ConfigMgmt
{
    public class AppConfigMgr
    {
        public enum ConfigScope { USER = 1, SYSTEM = 2 }

        private static string appCfgFileName = "AppSettings";
        public static string AppCfgFileName
        {
            get { return AppConfigMgr.appCfgFileName; }
            set { AppConfigMgr.appCfgFileName = value; }
        }
        

        private static AppConfigMgr instance = null;
        public static AppConfigMgr Instance
        {
            get
            {
                if (instance == null)
                    instance = new AppConfigMgr();
                return instance;
            }
        }

        ConfSettings userConfigSettings = new ConfSettings();
        ConfSettings appConfigSettings = new ConfSettings();
        string appCfgPath = "";
        string appCfgFn = "";
        string userCfgPath = "";
        string userCfgFn = "";

        public AppConfigMgr()
        {
            InitSettings();
        }

        private void InitSettings()
        {
            appCfgFileName = AppDomain.CurrentDomain.FriendlyName;
            
            userCfgPath = Environment.ExpandEnvironmentVariables(@"%APPDATA%\" + appCfgFileName);
            appCfgPath = Environment.ExpandEnvironmentVariables(@"%ALLUSERSPROFILE%\Application Data\" + appCfgFileName);

            userCfgFn = userCfgPath + "\\" + appCfgFileName + ".xml";
            appCfgFn = appCfgPath + "\\" + appCfgFileName + ".xml";

            PrepareDirectories();
        }

        private void PrepareDirectories()
        {
            if (!Directory.Exists(userCfgPath))
                Directory.CreateDirectory(userCfgPath);

            if (!Directory.Exists(appCfgPath))
                Directory.CreateDirectory(appCfgPath);
        }

        public string GetConfigValue(string id, ConfigScope scp)
        {
            if (scp == ConfigScope.SYSTEM)
                return (string)appConfigSettings[id];
            else if (scp == ConfigScope.USER)
                return (string)userConfigSettings[id];
            else
                throw new Exception("Unsupported scope");
        }

        public void SetConfigValue(string id, string value, ConfigScope scp)
        {
            if (scp == ConfigScope.SYSTEM)
            {
                if (appConfigSettings.ContainsKey(id))
                    appConfigSettings[id] = value;
                else
                    appConfigSettings.AddKey(id, value);
            }
            else if (scp == ConfigScope.USER)
            {
                if (userConfigSettings.ContainsKey(id))
                    userConfigSettings[id] = value;
                else
                    userConfigSettings.AddKey(id, value);
            }
        }

        public void PersistSettings()
        {
            userConfigSettings.SaveAsXml(userCfgFn);
            appConfigSettings.SaveAsXml(appCfgFn);
        }

        ~AppConfigMgr()
        {
            PersistSettings();
        }

    }
}
